import { addOutbuyProvider, updateOutbuyProvider, deleteById, getById, getOutbuyProviderList, queryBuyerApprovalList, approvalBuyer, queryBuyerAllById } from '../../services/outbuy/outbuyProvider';
import {message} from 'antd';
const R=require("ramda");

export default {
    namespace: 'outbuyProvider',
    state: {
        page: {},
        list: [],
        buyerList: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 获取境外贸易商信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getOutbuyProviderList({payload}, { call, put, select }) {
            const res = yield call(getOutbuyProviderList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据id查境外贸易商
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                    if(payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },

        /**
         * 删除境外贸易商
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteById({ payload }, { call, put, select }) {
            const res = yield call(deleteById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除商品供应商成功");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加境外贸易商
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *addOutbuyProvider({payload}, { call, put, select }) {
            const res = yield call(addOutbuyProvider, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加商品供应商成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新境外贸易商
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *updateOutbuyProvider({payload}, { call, put, select }) {
            const res = yield call(updateOutbuyProvider, payload);
            if (res.data.resultCode === "ok") {
                message.info("修改商品供应商成功");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 审核境外贸易商
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *approvalBuyer({payload}, { call, put, select }) {
            const res = yield call(approvalBuyer, payload);

            if (res.data.resultCode === "ok") {
                message.info("审核商品供应商成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**外商审批记录
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *queryBuyerApprovalList({payload}, { call, put, select }) {
            const res = yield call(queryBuyerApprovalList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取客户的商品供应商
         *
         * @param call
         * @param put
         * @param select
         */
            *queryBuyerAllById({ payload }, { call, put, select }) {
            const res = yield call(queryBuyerAllById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        buyerList: R.isNil(content) ? [] : content,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    }
};
